package com.yunhui.controller;

import com.yunhui.entity.User;
import com.yunhui.mapper.UserMapper;
import com.yunhui.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户的前端控制
 */
@Controller

public class userController {

    @Resource
    private UserService userService;


    @RequestMapping("index")
    public String index() {
        return "index";
    }

    @RequestMapping("houtai")
    public String index2() {
        return "/houtai/index";
    }

    @RequestMapping("houtat1")
    public String index3() {

        return "/houtai/welcome";
    }

    @RequestMapping("houtat2")
    public String index4() {
        return "pack";
    }


    @RequestMapping("login_sign")
    public String login_sign() {
        return "login_sign";
    }


    //登录验证
    @RequestMapping(value = "get_login")
    @ResponseBody
    public String get_login(@RequestParam("phone_email") String phone_email,
                            @RequestParam("password") String password,
                            HttpSession session) {



        User loguser = userService.selectByPhoneOrEmail(phone_email);
        if (loguser == null) {

            return "对不起！该用户不存在";
        }
        if (password.equals(loguser.getPassword())) {
            session.setAttribute("LoginUser", loguser);
            return "index";
        } else {

            return "密码错误";
        }

    }

    //注册用户
    @RequestMapping("save_user")
    public String save_user(String phone,String pwd,String email, HttpSession session) {

       User user = new User();
        user.setPhone(phone);
        user.setEmail(email);
        user.setPassword(pwd);
        user.setCreateDate(new Date());
        user.setImgurl("");
        user.setImgname("");
        user.setName("");
        user.setBirthday(new Date());
        if(userService.save(user)){
            session.setAttribute("LoginUser", user);
            return "index";
        }


        return "error";
    }




}
